﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using System.Collections.Specialized;
using System.Security.Permissions;
using System.Configuration;
using Microsoft.WindowsAzure.Storage;

namespace CloudTrace.Reader
{
    public abstract class CloudTraceReader
    {
        public const string StorageConnectionStringName = "DataConnectionString";

        private bool initialized;
        protected CloudStorageAccount storageAccount;
        protected string storageAccountSetting;
        protected bool usePlaintext;

        #region ctor

        [SecurityPermission(SecurityAction.Demand)]
        protected CloudTraceReader()
            : this(StorageConnectionStringName)
        {
        }

        [SecurityPermission(SecurityAction.Demand)]
        protected CloudTraceReader(string storageAccountSetting)
        {
            this.storageAccountSetting = storageAccountSetting;
        }
        #endregion

        protected virtual void Initialize()
        {
            if (!initialized)
            {
                try
                {
                    this.storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(storageAccountSetting));
                }
                catch (Exception ex)
                {
                    Type type = Type.GetTypeFromHandle(Type.GetTypeHandle(this));
                    throw new ApplicationException(type.FullName + ".ctor: Not able to read storage account setting or open the storage - " + ex.Message);
                }
                initialized = true;
            }
        }

        protected abstract void InitializeService();

    }
}
